<?php
require_once('sample.class.php');
$sample = new sample;

require_once($sample->paths["smarty_path"]->get_primary()."Smarty.class.php");

$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->caching = false;
$smarty->template_dir = $sample->paths["sample"]->get_primary();
$smarty->compile_dir = $sample->paths['smarty_compile_path']->get_primary();
$smarty->config = $sample->paths["sample"]->get_primary();
$smarty->cache_dir = $sample->paths['smarty_cache_path']->get_primary();
import_request_variables("p", "post_");
import_request_variables("g", "get_");


switch($get_action){
//action == input
	case "input":
		switch($post_form_action){
//action == input with form action == save
			case "save":
				if ($sample->set_by_array($post_record)){
//action == input with form action == save and check_validation returns true
					$sample->add_to_DB();
					$sample->clear_object();
					$smarty->assign('sample', $sample);
					$smarty->assign('flash_on', 1);
					$smarty->assign('flash', "record has been added");
					$smarty->assign('sample', $sample);
					$smarty->display($sample->paths['tpl_path']->get_primary()."head.tpl");
					$smarty->display('sample_input.tpl');
					$smarty->display($sample->paths['tpl_path']->get_primary()."foot.tpl");
//action == input with form action == save and check_validation returns false
				}else{
					$smarty->assign('sample', $sample);
					$smarty->display($sample->paths['tpl_path']->get_primary()."head.tpl");
					$smarty->display('sample_input.tpl');
					$smarty->display($sample->paths['tpl_path']->get_primary()."foot.tpl");
				}
			break;
			default:
//action == input with no form_action set
					$smarty->assign('sample', $sample);
					$smarty->display($sample->paths['tpl_path']->get_primary()."head.tpl");
					$smarty->display('sample_input.tpl');
					$smarty->display($sample->paths['tpl_path']->get_primary()."foot.tpl");
			break;
		}
	break;

//action == display_all
	case "display_all":
		$smarty->assign('list', $sample->get_list_DB('ORDER BY ROWID'));
		$smarty->display($sample->paths['tpl_path']->get_primary()."head.tpl");
		$smarty->display('sample_display_all.tpl');
		$smarty->display($sample->paths['tpl_path']->get_primary()."foot.tpl");
	break;

//action == display_one
	case "display_one":
//action == display_one and record_id set
		if (isset($get_record_id)){
			$sample->load_from_DB($get_record_id);
			$smarty->assign('sample', $sample);
//action == display_one and record_id not set
		}else{
			$sample->load_from_last_insert_DB();
			$smarty->assign('sample', $sample);
		}
		$smarty->display($sample->paths['tpl_path']->get_primary()."head.tpl");
		$smarty->display('sample_display_one.tpl');
		$smarty->display($sample->paths['tpl_path']->get_primary()."foot.tpl");
	break;

//action == edit
	case "edit":
//action == edit and record_id not set
		if (!isset($get_record_id)){
			header("Location: sample_control.php?action=display_all");
		}
		switch($post_form_action){
//action == edit and form_action == update
			case "update":
//action == edit and form_action == update and validate_all returns true
				if ($sample->set_by_array($post_record)){
					$sample->save_to_DB();
					$sample->load_from_DB($sample->vars['ROWID']->get_value());
					$smarty->assign('sample', $sample);
					$smarty->assign('flash_on', 1);
					$smarty->assign('flash', "record has been updated");
					$smarty->display($sample->paths['tpl_path']->get_primary()."head.tpl");
					$smarty->display('sample_display_one.tpl');
					$smarty->display($sample->paths['tpl_path']->get_primary()."foot.tpl");
//action == edit and form_action == update and validate_all returns false
				}else{
					$smarty->assign('sample', $sample);
					$smarty->display($sample->paths['tpl_path']->get_primary()."head.tpl");
					$smarty->display('sample_edit.tpl');
					$smarty->display($sample->paths['tpl_path']->get_primary()."foot.tpl");
				}
			break;
//action == edit and form_action not set
			default:
				if (isset($get_record_id)){
					$sample->load_from_DB($get_record_id);
				}else{
					$sample->load_from_last_insert_DB();
				}
				$smarty->assign('sample', $sample);
					$smarty->display($sample->paths['tpl_path']->get_primary()."head.tpl");
					$smarty->display('sample_edit.tpl');
					$smarty->display($sample->paths['tpl_path']->get_primary()."foot.tpl");
			break;
		}
	break;

//action == delete
	case "delete":
		switch($post_form_action){
//action == delete form_action == delete
			case "delete":
				$sample->delete_from_DB($get_record_id);
				$smarty->assign('flash_on', 1);
				$smarty->assign('flash', "record has been deleted");
				$smarty->assign('list', $sample->get_list_db('ORDER BY ROWID'));
				$smarty->display($sample->paths['tpl_path']->get_primary()."head.tpl");
				$smarty->display('sample_display_all.tpl');
				$smarty->display($sample->paths['tpl_path']->get_primary()."foot.tpl");
			break;
//action == delete and form_action not set
			default:
//action == delete and form_action not set and record_id set
				if (isset($get_record_id)){
					$sample->load_from_DB($get_record_id);
					$smarty->assign('sample', $sample);
					$smarty->display($sample->paths['tpl_path']->get_primary()."head.tpl");
					$smarty->display('sample_delete.tpl');
					$smarty->display($sample->paths['tpl_path']->get_primary()."foot.tpl");
//action == delete and form_action not set and record_id not set
				}else{
					$smarty->assign('flash_on', 1);
					$smarty->assign('flash', "no record selected");
					header("Location: sample_control.php?action=display_all");
				}
			break;
		}
	break;
}
?>
